package com.black.platform.user.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.black.common.auth.UserContextHolder;
import com.black.common.vo.ShareUserVo;
import com.black.common.web.ApiResponse;
import com.black.common.web.ApiResponseBuilder;
import com.black.common.web.PageQuery;
import com.black.platform.user.po.ShareUser;
import com.black.platform.user.service.handler.ShareUserHandlerService;
import com.black.platform.user.vo.ShareUserPositionVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author linwenfeng
 */
@RestController
@RequestMapping("shareUser")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareUserController {

    private final ShareUserHandlerService shareUserHandlerService;

    @PostMapping("search")
    public ApiResponse<IPage<ShareUser>> search(@RequestBody PageQuery<ShareUser> searchVo) {
        return ApiResponseBuilder.ok(shareUserHandlerService.search(searchVo));
    }

    @PostMapping("save")
    public ApiResponse<Boolean> save(@RequestBody ShareUserPositionVo shareUserPositionVo) {
        return ApiResponseBuilder.ok(shareUserHandlerService.saveOrUpdate(shareUserPositionVo));
    }

    @PostMapping("update")
    public ApiResponse<Boolean> update(@RequestBody ShareUserPositionVo shareUserPositionVo) {
        return ApiResponseBuilder.ok(shareUserHandlerService.saveOrUpdate(shareUserPositionVo));
    }


    @PutMapping("resetPassword")
    public ApiResponse<Boolean> resetPassword(@RequestParam("userId") Long userId) {
        return ApiResponseBuilder.ok(shareUserHandlerService.resetPassword(userId));
    }

    @PutMapping("close")
    public ApiResponse<Boolean> close(@RequestParam("userId") Long userId) {
        return ApiResponseBuilder.ok(shareUserHandlerService.close(userId));
    }

    @PutMapping("open")
    public ApiResponse<Boolean> open(@RequestParam("userId") Long userId) {
        return ApiResponseBuilder.ok(shareUserHandlerService.open(userId));
    }

    @GetMapping("findUserById/{userId}")
    public ApiResponse<ShareUserPositionVo> findUserById(@PathVariable("userId") Long userId) {
        return ApiResponseBuilder.ok(shareUserHandlerService.getShareUserPositionVoByUserId(userId));
    }

    @GetMapping("findUserByUsername")
    public ApiResponse<ShareUser> findUserByUsername(@RequestParam("username") String username) {
        return ApiResponseBuilder.ok(shareUserHandlerService.findUserByUsername(username));
    }

    @GetMapping("getMyself")
    public ApiResponse<ShareUser> getMyself() {
        ShareUser shareUser = shareUserHandlerService.getShareUserById(UserContextHolder.getUserId());
        shareUser.setPassword(null);
        return ApiResponseBuilder.ok(shareUser);
    }

    @GetMapping("getUserHavingRoleId/{userId}")
    public ApiResponse<Set<Long>> getShareUserHavingRoleIdByUserId(@PathVariable("userId") Long userId) {
        return ApiResponseBuilder.ok(shareUserHandlerService.getShareUserHavingRoleIdByUserId(userId));
    }

    @GetMapping("findUserByOtherId")
    public ApiResponse<ShareUser> findUserByOtherId(@RequestParam("otherId") String otherId,@RequestParam("idType")String idType){
        return ApiResponseBuilder.ok(shareUserHandlerService.findUserByOtherId(otherId,idType));
    }

    @PostMapping("register")
    public ApiResponse<ShareUser> register(@RequestBody ShareUser shareUser){
        return ApiResponseBuilder.ok(shareUserHandlerService.register(shareUser));
    }
}
